package com.balabala.gym_goods.controller.admin;

import com.balabala.gym_enum.ResultEnum;
//import com.balabala.gym_goods.pojo.dto.SpuQueryDto;
import com.balabala.gym_goods.pojo.dto.BaseDeleteDto;
import com.balabala.gym_goods.pojo.dto.SpuQueryDto;
import com.balabala.gym_goods.pojo.entity.Spu;
import com.balabala.gym_goods.pojo.entity.Spu;
//import com.balabala.gym_goods.pojo.vo.SpuQueryVo;
import com.balabala.gym_goods.pojo.vo.SpuListVo;
import com.balabala.gym_goods.pojo.vo.SpuQueryVo;
import com.balabala.gym_goods.service.SpuService;
import com.balabala.gym_goods.service.SpuService;
import com.balabala.result.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@CrossOrigin
@Api(tags = "商品表接口")
@RequestMapping("/admin/spu")
public class SpuController {

    @Autowired
    private SpuService spuService;

    @ApiOperation(value = "查询所有商品信息")
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public Result findAll() {
        List<Spu> spuList = spuService.list();
        return Result.ok(spuList);
    }

    @ApiOperation(value = "根据id查询对应的商品信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result find(@PathVariable("id") Long id) {
        return Result.ok(ResultEnum.GENERALLY_SUCCESS, spuService.getById(id));
    }

    @ApiOperation(value = "添加商品信息")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@RequestBody Spu spu) {
        log.info("添加商品分类信息");
        spu.setCreateTime(new Date());
        spu.setUpdateTime(new Date());
        return Result.ok(spuService.save(spu));
    }

    @ApiOperation(value = "根据id删除商品信息")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Result deleteById(@PathVariable Long id) {
        boolean result = spuService.removeById(id);
        return Result.judge(result);
    }

    @ApiOperation(value = "修改商品信息")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody Spu spu) {
        spu.setUpdateTime(new Date());
        boolean result = spuService.updateById(spu);
        return Result.judge(result);
    }

    @ApiOperation("带条件查询对应商品")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(SpuQueryDto spuQueryDto) {
        PageHelper.startPage(spuQueryDto.getPage(), spuQueryDto.getLimit());
        List<SpuListVo> spuQueryList = spuService.findSpuByQueryDto(spuQueryDto);
        PageInfo<SpuListVo> pageInfo = new PageInfo<>(spuQueryList);
        return Result.ok(spuQueryList, pageInfo.getTotal());
    }

    @ApiOperation("批量删除")
    @RequestMapping(value = "/deleteByIds",method = RequestMethod.POST)
    public Result deleteByIds(@RequestBody BaseDeleteDto baseDeleteDto){
        boolean result = spuService.removeByIds(baseDeleteDto.getIds());
        return Result.ok(result);
    }

}
